package com.googlecode.trabhci2011.stockmarket.server;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Vector;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.trabhci2011.stockmarket.client.StockService;
import com.googlecode.trabhci2011.stockmarket.shared.StockDataDefs;
import com.googlecode.trabhci2011.stockmarket.shared.StockDataEntry;

public class StockServiceImpl extends RemoteServiceServlet implements StockService {
	SimpleDateFormat df = new SimpleDateFormat(StockDataDefs.DATE_FORMAT_STRING);
	SimpleDateFormat fdf = new SimpleDateFormat(StockDataDefs.FILE_DATE_FORMAT_STRING);
	private HashMap<String, Vector<StockDataEntry>> stockData = new HashMap<String, Vector<StockDataEntry>>();
	StockDataReader reader = new StockDataReader(stockData);
	@Override
	public Vector<String> stockServer(HashMap<String, String> data) throws IllegalArgumentException {
		Date dateTo = new Date();
		Date dateFrom = new Date();		
		if(data.get(StockDataDefs.END_DATE_NAME) != null){
			try {
				dateTo = df.parse(data.get(StockDataDefs.END_DATE_NAME));
				System.out.println("End date :" + dateTo);
			} catch (ParseException e) {
				throw new IllegalArgumentException(e);
			}
		}
		if(data.get(StockDataDefs.START_DATE_NAME) != null){
			try {
				dateFrom = df.parse(data.get(StockDataDefs.START_DATE_NAME));
				System.out.println("Start date :" + dateFrom);
			} catch (ParseException e) {
				throw new IllegalArgumentException(e);
			}
		}
		
		String indexSeries = data.get(StockDataDefs.INDEX_NAMES);
		//If no box checked
		if(indexSeries == null){
			//Return nothing
			return new Vector<String>();
		}
		Vector<String> out = new Vector<String>();
		int i = 0;
		for(String index : indexSeries.split(" ")){
			//Time contraints?????
			System.out.println("Index " + (i++) + ": " + index);
			Vector<StockDataEntry> v_StockDataEntry = stockData.get(index);
			if(v_StockDataEntry != null){
				Iterator<StockDataEntry> it = v_StockDataEntry.iterator();
				while(it.hasNext()){				
					StockDataEntry dataEntry = it.next();
					if(inTime(dataEntry, dateFrom, dateTo)){
						out.add(dataEntry.toString());
					}
				}
			}
		}
		
		Iterator<String> it = data.keySet().iterator();
		while(it.hasNext()){
			String itnext = it.next();
			//System.out.println(itnext + " :" + data.get(itnext));
			out.add(data.get(itnext));
		}
		return out;
	}


	private boolean inTime(StockDataEntry data, Date start, Date end){
		if (start==null) {
			return true;
		}
		if (end==null) {
			return true;
		}
		Date date;
		try {
			date = fdf.parse(data.getDate());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			throw new IllegalArgumentException(e);
		}
		if (date.before(start)) {
			return false;
		}
		if (date.after(end)){
			return false;
		}
		return true;
	}

}